package com.jinghao.tws.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.jinghao.core.common.exception.JingHaoCustomerException;
import com.jinghao.core.common.ftpclient.JhFtpTool;
import com.jinghao.core.common.service.impl.JingHaoBaseServiceImpl;
import com.jinghao.core.common.util.JhHttpClientUtil;
import com.jinghao.core.common.util.JingHaoPageInfo;
import com.jinghao.core.common.util.cnd.JhCnd;
import com.jinghao.core.common.util.cnd.JhUpdate;
import com.jinghao.tws.entity.WvCamera;
import com.jinghao.tws.entity.WvCloudAccessConfig;
import com.jinghao.tws.entity.WvFlow;
import com.jinghao.tws.entity.WvFlow.EWvFlow;
import com.jinghao.tws.entity.WvPallet;
import com.jinghao.tws.entity.WvPallet.EWvPallet;
import com.jinghao.tws.entity.WvProcess;
import com.jinghao.tws.entity.WvProcess.EWvProcess;
import com.jinghao.tws.entity.WvProcessConfig;
import com.jinghao.tws.entity.WvProduct;
import com.jinghao.tws.entity.WvProduct.EWvProduct;
import com.jinghao.tws.entity.WvUploadTaskInfo;
import com.jinghao.tws.entity.WvUploadTaskInfo.EWvUploadTaskInfo;
import com.jinghao.tws.service.IWvCameraService;
import com.jinghao.tws.service.IWvCloudAccessConfigService;
import com.jinghao.tws.service.IWvFlowService;
import com.jinghao.tws.service.IWvPalletService;
import com.jinghao.tws.service.IWvProcessConfigService;
import com.jinghao.tws.service.IWvProcessService;
import com.jinghao.tws.service.IWvProductService;
import com.jinghao.tws.service.IWvPublicSynchronousService;
import com.jinghao.tws.service.IWvUploadTaskInfoService;
import com.jinghao.tws.service.nos.NosService;

@Service("wvPublicSynchronousService")
public class WvPublicSynchronousServiceImpl extends JingHaoBaseServiceImpl implements IWvPublicSynchronousService {


	//==================定制内容开始======================
//在这里修改代码
	@Resource
	private IWvCloudAccessConfigService wvCloudAccessConfigService;

	@Resource
	private IWvCameraService  wvCameraService;

	@Resource
	private IWvProcessConfigService wvProcessConfigService;

	@Resource
	private IWvFlowService  wvFlowService;

	@Resource
	private IWvUploadTaskInfoService  wvUploadTaskInfoService;

	@Resource
	private IWvPalletService wvPalletService;

	@Resource
	private IWvProcessService wvProcessService;

	@Resource
	private IWvProductService wvProductService;

	@Resource
	private NosService nosService;



	@Override
	public void synchronousAllPalletTjData() {
		WvCloudAccessConfig model=new WvCloudAccessConfig();
		//第一步：查询是否配置云段接口及云平台验证
		Map<String,Object> dateInfo=this.cloudLogin(model);
		model=(WvCloudAccessConfig)dateInfo.get("wvCloudAccessConfig");
		JhHttpClientUtil hd = new JhHttpClientUtil();
		Map<String,String> map=new HashMap<String,String>();
		Object obj=dateInfo.get("loginInfo");
		Map<String,Object> loginInfo=(Map<String,Object>)obj;
		map.put("accessTokenData", JSON.toJSONString(loginInfo));
		WvPallet newModel=new WvPallet();
		newModel.setPage(null);
		List<Map<String,Object>> list=this.wvPalletService.queryMapListById(newModel, "queryMapListAllWvPalletTJ");
		if(list!=null&&list.size()>0){
			map.put("jhPublicData", JSON.toJSONString(list));
			String dtUrl="app/ctws/public/syn/synchronousAllPalletTjData";
			String httpUrl=model.getAddress();
			if(httpUrl.substring(httpUrl.length()-1, httpUrl.length()).equals("/")){
				httpUrl=model.getAddress()+""+dtUrl;
			}else{
				httpUrl=model.getAddress()+"/"+dtUrl;
			}
			String msg= hd.post(httpUrl, map);
			this.checkResult(msg);
			list.clear();
		}
		list=null;

	}

	@Override
	public void updateSynchronousCamera() {
		WvCloudAccessConfig model=new WvCloudAccessConfig();
		//第一步：查询是否配置云段接口及云平台验证
		Map<String,Object> dateInfo=this.cloudLogin(model);
		model=(WvCloudAccessConfig)dateInfo.get("wvCloudAccessConfig");
		JhHttpClientUtil hd = new JhHttpClientUtil();
		Map<String,String> map=new HashMap<String,String>();
		Object obj=dateInfo.get("loginInfo");
		Map<String,Object> loginInfo=(Map<String,Object>)obj;
		map.put("accessTokenData", JSON.toJSONString(loginInfo));
		WvCamera newModel=new WvCamera();
		newModel.setPage(null);
		List<Map<String,Object>> list=this.wvCameraService.queryMapList(newModel);
		if(list!=null&&list.size()>0){
			map.put("jhPublicData", JSON.toJSONString(list));
			String dtUrl="app/ctws/public/syn/updateSynchronousCamera";
			String httpUrl=model.getAddress();
			if(httpUrl.substring(httpUrl.length()-1, httpUrl.length()).equals("/")){
				httpUrl=model.getAddress()+""+dtUrl;
			}else{
				httpUrl=model.getAddress()+"/"+dtUrl;
			}
			String msg= hd.post(httpUrl, map);
			this.checkResult(msg);
			list.clear();
		}
		list=null;
	}

	@Override
	public void updateSynchronousFlow() {
		WvCloudAccessConfig model=new WvCloudAccessConfig();
		//第一步：查询是否配置云段接口及云平台验证
		Map<String,Object> dateInfo=this.cloudLogin(model);
		model=(WvCloudAccessConfig)dateInfo.get("wvCloudAccessConfig");
		JhHttpClientUtil hd = new JhHttpClientUtil();
		Map<String,String> map=new HashMap<String,String>();
		Object obj=dateInfo.get("loginInfo");
		Map<String,Object> loginInfo=(Map<String,Object>)obj;
		map.put("accessTokenData", JSON.toJSONString(loginInfo));

	    WvFlow wvFlow=new WvFlow();
		JhCnd cnd=new JhCnd();
		cnd.andEquals("status");
		wvFlow.setStatus(EWvFlow.status_1.getKey());
		wvFlow.setPage(null);
		wvFlow.setJhCnd(cnd);
		List<Map<String,Object>> list=this.wvFlowService.queryMapList(wvFlow);
		String code="";
		List<Map<String,Object>> configList=null;
		WvProcessConfig wvProcessConfig=new WvProcessConfig();
		if(list!=null&&list.size()>0){
			for(Map<String,Object> tempMap:list){
				code=tempMap.get("code").toString();
				wvProcessConfig.setFcode(code);
				wvProcessConfig.setPage(null);
				cnd.reset();
				cnd.andEquals("fcode");
				wvProcessConfig.setJhCnd(cnd);
				configList=this.wvProcessConfigService.queryMapList(wvProcessConfig);
				tempMap.put("wvProcessConfig", configList);
			}
			map.put("jhPublicData", JSON.toJSONString(list));
		}else{
			throw new JingHaoCustomerException("本地未配置流程信息，不需要进行同步!");
		}
		String dtUrl="app/ctws/public/syn/updateSynchronousFlow";
		String httpUrl=model.getAddress();
		if(httpUrl.substring(httpUrl.length()-1, httpUrl.length()).equals("/")){
			httpUrl=model.getAddress()+""+dtUrl;
		}else{
			httpUrl=model.getAddress()+"/"+dtUrl;
		}
		String msg= hd.post(httpUrl, map);
		this.checkResult(msg);
		list.clear();
		list=null;
	}

	public void checkResult(String msg){
		 if(msg==null||msg.trim().equals("")){
			 throw new JingHaoCustomerException("云端连接失败，请联系系统管理员!");
		 }
		 Map<String,Object> resultMap=(Map<String,Object>)JSON.parseObject(msg);
         if(resultMap!=null&&resultMap.get("success").toString().equals("true")){
        	 //操作成功
         }else if(resultMap!=null&&resultMap.get("success").toString().equals("false")){
        	 logger.error("云端连接失败，请联系系统管理员!错误原因："+resultMap.get("messages").toString());
        	 throw new JingHaoCustomerException("云端连接失败，请联系系统管理员!错误原因："+resultMap.get("messages").toString());
         }else{
        	 logger.error("云端连接失败，请联系系统管理员!");
        	 throw new JingHaoCustomerException("云端连接失败，请联系系统管理员!");
         }
         resultMap.clear(); 
         resultMap=null;

	}

	/**
	 * 云平台登录认证
	 */
	public Map<String,Object> cloudLogin(WvCloudAccessConfig model){
		List<WvCloudAccessConfig> list=this.wvCloudAccessConfigService.queryModelList(model);
		if(list==null||list.size()<=0){
			throw new JingHaoCustomerException("请先配置云平台账户，再进行数据同步操作!");
		}
		model=list.get(0);
		return this.cloudLoginCheck(model);
	}
	/**
	 * 云平台登录认证
	 * @param userName
	 * @param password
	 * @param httpUrl
	 * @return
	 */
	public Map<String,Object> cloudLoginCheck(WvCloudAccessConfig model){

		String dtUrl="public/ctws/login";
		String httpUrl=model.getAddress();
		if(httpUrl.substring(httpUrl.length()-1, httpUrl.length()).equals("/")){
			httpUrl=model.getAddress()+""+dtUrl;
		}else{
			httpUrl=model.getAddress()+"/"+dtUrl;
		}
		Map<String,String> map=new HashMap<String,String>();
		JhHttpClientUtil hd = new JhHttpClientUtil();
		map.put("jhactCode", model.getUsername());
		map.put("jhactPassword", model.getPassword());
        String msg= hd.post(httpUrl, map);
        Map<String,Object> resultMap=(Map<String,Object>)JSON.parseObject(msg);
        Map<String,Object> dateMap=null;
        if(resultMap!=null&&resultMap.get("success").toString().equals("true")){
        	dateMap=(Map<String,Object>) resultMap.get("data");
        }else if(resultMap!=null&&resultMap.get("success").toString().equals("false")){
        	throw new JingHaoCustomerException("云端连接失败，请联系系统管理员!错误原因："+resultMap.get("messages").toString());
        }else{
        	throw new JingHaoCustomerException("云端连接失败，请联系系统管理员!");
        }
        Map<String,Object> dataMapInfo=new HashMap<String,Object>();
        dataMapInfo.put("wvCloudAccessConfig", model);
        dataMapInfo.put("loginInfo", dateMap);
        return  dataMapInfo;
	}

	public void synchronousAllPalletFileData() {
		WvCloudAccessConfig model=new WvCloudAccessConfig();
		//第一步：查询是否配置云段接口及云平台验证
		Map<String,Object> dateInfo=this.cloudLogin(model);
		model=(WvCloudAccessConfig)dateInfo.get("wvCloudAccessConfig");
		JhHttpClientUtil hd = new JhHttpClientUtil();
		Map<String,String> map=new HashMap<String,String>();
		Object obj=dateInfo.get("loginInfo");
		Map<String,Object> loginInfo=(Map<String,Object>)obj;
		map.put("accessTokenData", JSON.toJSONString(loginInfo));
		JhFtpTool ftpTool=new JhFtpTool(model.getFtpAddress(),model.getFtpPort(),model.getFtpUsername(),model.getFtpPassword());


		 //第一步：同步合并视频文件
		this.updateSynchronousWvPalletFile(ftpTool,model,map,hd);
		if(ftpTool!=null){
			ftpTool=null;
		}
	}

	@Override
	public void synchronousAllProcessFileData() {
		WvCloudAccessConfig model=new WvCloudAccessConfig();
		//第一步：查询是否配置云段接口及云平台验证
		Map<String,Object> dateInfo=this.cloudLogin(model);
		model=(WvCloudAccessConfig)dateInfo.get("wvCloudAccessConfig");
		JhHttpClientUtil hd = new JhHttpClientUtil();
		Map<String,String> map=new HashMap<String,String>();
		Object obj=dateInfo.get("loginInfo");
		Map<String,Object> loginInfo=(Map<String,Object>)obj;
		map.put("accessTokenData", JSON.toJSONString(loginInfo));
		JhFtpTool ftpTool=new JhFtpTool(model.getFtpAddress(),model.getFtpPort(),model.getFtpUsername(),model.getFtpPassword());

		//第二步：同步环节视频文件
		this.updateSynchronousWvProcessFile(ftpTool,model,map,hd);
		if(ftpTool!=null){
			ftpTool=null;
		}

	}

	@Override
	public void synchronousAllProductFileData() {
		WvCloudAccessConfig model=new WvCloudAccessConfig();
		//第一步：查询是否配置云段接口及云平台验证
		Map<String,Object> dateInfo=this.cloudLogin(model);
		model=(WvCloudAccessConfig)dateInfo.get("wvCloudAccessConfig");
		JhHttpClientUtil hd = new JhHttpClientUtil();
		Map<String,String> map=new HashMap<String,String>();
		Object obj=dateInfo.get("loginInfo");
		Map<String,Object> loginInfo=(Map<String,Object>)obj;
		map.put("accessTokenData", JSON.toJSONString(loginInfo));
		JhFtpTool ftpTool=new JhFtpTool(model.getFtpAddress(),model.getFtpPort(),model.getFtpUsername(),model.getFtpPassword());

		//第三步：同步短损视频文件
		this.updateSynchronousWvProductFile(ftpTool,model,map,hd);
		if(ftpTool!=null){
			ftpTool=null;
		}
	}

	public boolean fileUploadToCloud(JhFtpTool ftpTool,WvCloudAccessConfig model,String fileName,String newFileName){
		return fileUploadToCloudNos(model, fileName, newFileName);
	}

	public boolean fileUploadToCloudNos(WvCloudAccessConfig model, String fileName,String newFileName) {
		String localFileDirectory = model.getLocalFileDirectory().replaceAll("\\\\", "/");
		if (localFileDirectory.substring(localFileDirectory.length() - 1, localFileDirectory.length()).equals("/")) {
			localFileDirectory = localFileDirectory + "" + fileName;
		} else {
			localFileDirectory = localFileDirectory + "/" + fileName;
		}
		return nosService.uploadObject(localFileDirectory, newFileName);
	}

	public boolean fileUploadToCloudFtp(JhFtpTool ftpTool,WvCloudAccessConfig model,String fileName,String newFileName){
		   boolean state=false;
		   String localFileDirectory=model.getLocalFileDirectory().replaceAll("\\\\", "/");
		   if(localFileDirectory.substring(localFileDirectory.length()-1, localFileDirectory.length()).equals("/")){
				localFileDirectory=localFileDirectory+""+fileName;
		   }else{
				localFileDirectory=localFileDirectory+"/"+fileName;
		   }
		   this.logger.info("---localFileDirectory---:"+localFileDirectory);
		   try{
		        ftpTool.connect();
		        try
		        {
		        	if(newFileName!=null&&!newFileName.trim().equals("")){
		        		ftpTool.resumeUpload(localFileDirectory, "/files/",newFileName);
		        	}else{
		        		ftpTool.resumeUpload(localFileDirectory, "/files/");
		        		this.logger.info("---localFileDirectory2---:"+localFileDirectory);
		        	}
		            state=true;
		        }catch (Exception e) {
		             this.logger.error("文件上传异常："+e.getMessage());
		             e.printStackTrace();
		        } finally{
		        //	ftpTool.close();
		        	return state;
		        }
		    } catch (Exception e) {
		    	 this.logger.error("文件上传异常："+e.getMessage());
		    	 e.printStackTrace();
		    } finally {
		    	return state;
	        }
	}

	public void updateSynchronousWvPalletFile(JhFtpTool ftpTool,WvCloudAccessConfig model,Map<String,String> dateMap,JhHttpClientUtil hd){
		WvUploadTaskInfo  wvUploadTaskInfo=new WvUploadTaskInfo();
		String dtUrl="app/ctws/public/syn/updateSynchronousPalletFileData";
		JhCnd cnd=new JhCnd();
		cnd.append(" and work_uuid in (select pallet_uuid from WV_PALLET where upload_Status="+EWvPallet.uploadStatus_3.getKey()+")");
		cnd.andEquals("taskType");
		wvUploadTaskInfo.setTaskType(EWvUploadTaskInfo.taskType_7.getKey());
		wvUploadTaskInfo.setJhCnd(cnd);
		JingHaoPageInfo page=new JingHaoPageInfo();
		page.setLimit(5);
		wvUploadTaskInfo.setPage(page);
		List<String> uuidList=new ArrayList<String>();
		List<String> taskUuidList=new ArrayList<String>();
		List<Map<String,Object>> dataMapList=new ArrayList<Map<String,Object>>();
		List<Map<String,Object>> list=this.wvUploadTaskInfoService.queryMapListById(wvUploadTaskInfo, "queryMapListToWorkUuid");
		WvPallet wvPallet=new WvPallet();
		JhUpdate jhUpdate=new  JhUpdate();
		int i=0;
		List<Map<String,Object>> listPallet=null;
		String fileName=null;
		String newFileName=null;
		for(Map<String,Object> map:list){
			uuidList.add(map.get("workUuid").toString());
			taskUuidList.add(map.get("taskUuid").toString());
		}
		if(uuidList.size()>0){
			cnd.reset();
			wvPallet.setStrList(uuidList);
			cnd.andIn("palletUuid", "strList", uuidList);
			wvPallet.setJhCnd(cnd);
			wvPallet.setPage(null);
			listPallet=this.wvPalletService.queryMapListById(wvPallet,"queryMapListToCloudFileData");
			for(Map<String,Object> tempMap:listPallet){
				//第一步：同步文件
				fileName=tempMap.get("urlLink").toString();
				fileName=fileName.substring(fileName.lastIndexOf("/")+1, fileName.length());
				newFileName=tempMap.get("palletId").toString()+"combine"+".mp4";
				if(!this.fileUploadToCloud(ftpTool, model, fileName,newFileName)){
					throw new JingHaoCustomerException("视频同步失败!");
				}
				tempMap.put("newFileName", newFileName);
				//第二步：同步数据
				dataMapList.clear();
				dataMapList.add(tempMap);
				dateMap.put("jhPublicData", JSON.toJSONString(dataMapList));
				this.saveSynchronousDate(model, dateMap, hd,dtUrl);
			}

			//删除任务记录
			wvUploadTaskInfo.setStrList(taskUuidList);
			cnd.reset();
			cnd.andIn("taskUuid", "strList", taskUuidList);
			wvUploadTaskInfo.setJhCnd(cnd);
			this.wvUploadTaskInfoService.deleteSame(wvUploadTaskInfo);

			//更新托盘状态
			cnd.reset();
			wvPallet.setStrList(uuidList);
			cnd.andIn("palletUuid", "strList", uuidList);
			wvPallet.setJhCnd(cnd);
			jhUpdate.add("fileUploadStatus").add("fileUploadTime");
			wvPallet.setFileUploadStatus(EWvPallet.fileUploadStatus_3.getKey());
			wvPallet.setFileUploadTime(new Date());
			wvPallet.setJhUpdate(jhUpdate);
			this.wvPalletService.updateSame(wvPallet);
		}
		list.clear();
		list=null;
	}

	public void updateSynchronousWvProcessFile(JhFtpTool ftpTool,WvCloudAccessConfig model,Map<String,String> dateMap,JhHttpClientUtil hd){
		WvUploadTaskInfo  wvUploadTaskInfo=new WvUploadTaskInfo();
		String dtUrl="app/ctws/public/syn/updateSynchronousProcessFileData";
		JhCnd cnd=new JhCnd();
		cnd.append(" and work_uuid in (select process_Uuid from WV_PROCESS where upload_Status="+EWvProcess.uploadStatus_3.getKey()+")");
		cnd.andEquals("taskType");
		wvUploadTaskInfo.setTaskType(EWvUploadTaskInfo.taskType_6.getKey());
		wvUploadTaskInfo.setJhCnd(cnd);
		JingHaoPageInfo page=new JingHaoPageInfo();
		page.setLimit(5);
		wvUploadTaskInfo.setPage(page);
		List<String> uuidList=new ArrayList<String>();
		List<String> taskUuidList=new ArrayList<String>();
		List<Map<String,Object>> dataMapList=new ArrayList<Map<String,Object>>();
		List<Map<String,Object>> list=this.wvUploadTaskInfoService.queryMapListById(wvUploadTaskInfo, "queryMapListToWorkUuid");
		WvProcess wvProcess=new WvProcess();
		JhUpdate jhUpdate=new  JhUpdate();
		int i=0;
		List<Map<String,Object>> listPallet=null;
		String fileName=null;
		String newFileName=null;
		for(Map<String,Object> map:list){
			uuidList.add(map.get("workUuid").toString());
			taskUuidList.add(map.get("taskUuid").toString());
		}
		if(uuidList.size()>0){
			cnd.reset();
			wvProcess.setStrList(uuidList);
			cnd.andIn("processUuid", "strList", uuidList);
			wvProcess.setJhCnd(cnd);
			wvProcess.setPage(null);
			listPallet=this.wvProcessService.queryMapListById(wvProcess,"queryMapListToCloudFileData");
			for(Map<String,Object> tempMap:listPallet){
				//第一步：同步文件
				fileName=tempMap.get("urlLink").toString();
				fileName=fileName.substring(fileName.lastIndexOf("/")+1, fileName.length());
				//newFileName=fileName;
				newFileName=tempMap.get("fpalletId").toString()+tempMap.get("fprocessId").toString()+".mp4";
				if(!this.fileUploadToCloud(ftpTool, model, fileName, newFileName)){
					throw new JingHaoCustomerException("视频同步失败!");
				}
				tempMap.put("newFileName", newFileName);
				//第二步：同步数据
				dataMapList.clear();
				dataMapList.add(tempMap);
				dateMap.put("jhPublicData", JSON.toJSONString(dataMapList));
				this.saveSynchronousDate(model, dateMap, hd,dtUrl);
			}

			//删除任务记录
			wvUploadTaskInfo.setStrList(taskUuidList);
			cnd.reset();
			cnd.andIn("taskUuid", "strList", taskUuidList);
			wvUploadTaskInfo.setJhCnd(cnd);
			this.wvUploadTaskInfoService.deleteSame(wvUploadTaskInfo);

			//更新托盘状态
			cnd.reset();
			wvProcess.setStrList(uuidList);
			cnd.andIn("processUuid", "strList", uuidList);
			wvProcess.setJhCnd(cnd);
			jhUpdate.add("fileUploadStatus").add("fileUploadTime");
			wvProcess.setFileUploadStatus(EWvPallet.fileUploadStatus_3.getKey());
			wvProcess.setFileUploadTime(new Date());
			wvProcess.setJhUpdate(jhUpdate);
			this.wvProcessService.updateSame(wvProcess);
		}
		list.clear();
		list=null;
	}

	public void updateSynchronousWvProductFile(JhFtpTool ftpTool,WvCloudAccessConfig model,Map<String,String> dateMap,JhHttpClientUtil hd){
		WvUploadTaskInfo  wvUploadTaskInfo=new WvUploadTaskInfo();
		String dtUrl="app/ctws/public/syn/updateSynchronousProductFileData";
		JhCnd cnd=new JhCnd();
		cnd.append(" and work_uuid in (select product_uuid from WV_PRODUCT where upload_Status="+EWvProduct.uploadStatus_3.getKey()+")");
		cnd.andEquals("taskType");
		wvUploadTaskInfo.setTaskType(EWvUploadTaskInfo.taskType_8.getKey());
		wvUploadTaskInfo.setJhCnd(cnd);
		JingHaoPageInfo page=new JingHaoPageInfo();
		page.setLimit(5);
		wvUploadTaskInfo.setPage(page);
		List<String> uuidList=new ArrayList<String>();
		List<String> taskUuidList=new ArrayList<String>();
		List<Map<String,Object>> dataMapList=new ArrayList<Map<String,Object>>();
		List<Map<String,Object>> list=this.wvUploadTaskInfoService.queryMapListById(wvUploadTaskInfo, "queryMapListToWorkUuid");
		WvProduct wvProduct=new WvProduct();
		JhUpdate jhUpdate=new  JhUpdate();
		int i=0;
		List<Map<String,Object>> listPallet=null;
		String fileName=null;
		String newFileName=null;
		for(Map<String,Object> map:list){
			uuidList.add(map.get("workUuid").toString());
			taskUuidList.add(map.get("taskUuid").toString());
		}
		if(uuidList.size()>0){
			cnd.reset();
			wvProduct.setStrList(uuidList);
			cnd.andIn("productUuid", "strList", uuidList);
			wvProduct.setJhCnd(cnd);
			wvProduct.setPage(null);
			listPallet=this.wvProductService.queryMapListById(wvProduct,"queryMapListToCloudFileData");
			for(Map<String,Object> tempMap:listPallet){
				//第一步：同步文件
				fileName=tempMap.get("urlLink").toString();
				fileName=fileName.substring(fileName.lastIndexOf("/")+1, fileName.length());
				//newFileName=fileName;
				newFileName=tempMap.get("fpalletId").toString()+tempMap.get("productId").toString()+".mp4";
				if(!this.fileUploadToCloud(ftpTool, model, fileName, newFileName)){
					throw new JingHaoCustomerException("视频同步失败!");
				}
				tempMap.put("newFileName", newFileName);
				//第二步：同步数据
				dataMapList.clear();
				dataMapList.add(tempMap);
				dateMap.put("jhPublicData", JSON.toJSONString(dataMapList));
				this.saveSynchronousDate(model, dateMap, hd,dtUrl);
			}

			//删除任务记录
			wvUploadTaskInfo.setStrList(taskUuidList);
			cnd.reset();
			cnd.andIn("taskUuid", "strList", taskUuidList);
			wvUploadTaskInfo.setJhCnd(cnd);
			this.wvUploadTaskInfoService.deleteSame(wvUploadTaskInfo);

			//更新托盘状态
			cnd.reset();
			wvProduct.setStrList(uuidList);
			cnd.andIn("productUuid", "strList", uuidList);
			wvProduct.setJhCnd(cnd);
			jhUpdate.add("fileUploadStatus").add("fileUploadTime");
			wvProduct.setFileUploadStatus(EWvPallet.fileUploadStatus_3.getKey());
			wvProduct.setFileUploadTime(new Date());
			wvProduct.setJhUpdate(jhUpdate);
			this.wvProductService.updateSame(wvProduct);
		}
		list.clear();
		list=null;
	}



	@Override
	public void updateSynchronousAllPalletData() {
		WvCloudAccessConfig model=new WvCloudAccessConfig();
		//第一步：查询是否配置云段接口及云平台验证
		Map<String,Object> dateInfo=this.cloudLogin(model);
		model=(WvCloudAccessConfig)dateInfo.get("wvCloudAccessConfig");
		JhHttpClientUtil hd = new JhHttpClientUtil();
		Map<String,String> map=new HashMap<String,String>();
		Object obj=dateInfo.get("loginInfo");
		Map<String,Object> loginInfo=(Map<String,Object>)obj;
		map.put("accessTokenData", JSON.toJSONString(loginInfo));

	    //第一步：先同步托盘数据
		this.updateSynchronousWvPallet(model,map,hd);
		//第二步：同步环节数据
		this.updateSynchronousWvProcess(model,map,hd);
		//第三步：同步产品数据
		this.updateSynchronousWvProduct(model,map,hd);

	}
	/**
	 * 先同步托盘数据
	 * @param dateInfo
	 */
	public void updateSynchronousWvPallet(WvCloudAccessConfig model,Map<String,String> dateMap,JhHttpClientUtil hd){
		WvUploadTaskInfo  wvUploadTaskInfo=new WvUploadTaskInfo();
		JingHaoPageInfo page=new JingHaoPageInfo();
		page.setLimit(200);
		String dtUrl="app/ctws/public/syn/updateSynchronousPallet";
		JhCnd cnd=new JhCnd();
		cnd.andEquals("taskType");
		wvUploadTaskInfo.setTaskType(EWvUploadTaskInfo.taskType_3.getKey());
		wvUploadTaskInfo.setJhCnd(cnd);
		wvUploadTaskInfo.setPage(page);
		List<String> uuidList=new ArrayList<String>();
		List<String> taskUuidList=new ArrayList<String>();
		List<Map<String,Object>> list=this.wvUploadTaskInfoService.queryMapListById(wvUploadTaskInfo, "queryMapListToWorkUuid");
		WvPallet wvPallet=new WvPallet();
		JhUpdate jhUpdate=new  JhUpdate();
		int i=0;
		List<Map<String,Object>> listPallet=null;
		for(Map<String,Object> map:list){
			uuidList.add(map.get("workUuid").toString());
			taskUuidList.add(map.get("taskUuid").toString());
			i++;
			if(i==20){
				cnd.reset();
				wvPallet.setStrList(uuidList);
				cnd.andIn("palletUuid", "strList", uuidList);
				wvPallet.setJhCnd(cnd);
				wvPallet.setPage(null);
				listPallet=this.wvPalletService.queryMapListById(wvPallet,"queryMapListToCloud");
				if(listPallet!=null&&listPallet.size()>0){
					dateMap.put("jhPublicData", JSON.toJSONString(listPallet));
					this.saveSynchronousDate(model, dateMap, hd,dtUrl);
				}

				wvUploadTaskInfo.setStrList(taskUuidList);
				cnd.reset();
				cnd.andIn("taskUuid", "strList", taskUuidList);
				wvUploadTaskInfo.setJhCnd(cnd);
				this.wvUploadTaskInfoService.deleteSame(wvUploadTaskInfo);

				//更新托盘状态
				cnd.reset();
				wvPallet.setStrList(uuidList);
				cnd.andIn("palletUuid", "strList", uuidList);
				wvPallet.setJhCnd(cnd);
				jhUpdate.add("uploadStatus").add("uploadTime");
				wvPallet.setUploadStatus(EWvPallet.uploadStatus_3.getKey());
				wvPallet.setUploadTime(new Date());
				wvPallet.setJhUpdate(jhUpdate);
				this.wvPalletService.updateSame(wvPallet);

				taskUuidList.clear();
				uuidList.clear();
				i=0;
			}
		}
		if(i>0){
			cnd.reset();
			wvPallet.setStrList(uuidList);
			cnd.andIn("palletUuid", "strList", uuidList);
			wvPallet.setJhCnd(cnd);
			wvPallet.setPage(null);
			listPallet=this.wvPalletService.queryMapListById(wvPallet,"queryMapListToCloud");
			if(listPallet!=null&&listPallet.size()>0){
				dateMap.put("jhPublicData", JSON.toJSONString(listPallet));
				this.saveSynchronousDate(model, dateMap, hd,dtUrl);
			}

			wvUploadTaskInfo.setStrList(taskUuidList);
			cnd.reset();
			cnd.andIn("taskUuid", "strList", taskUuidList);
			wvUploadTaskInfo.setJhCnd(cnd);
			this.wvUploadTaskInfoService.deleteSame(wvUploadTaskInfo);


			//更新托盘状态
			cnd.reset();
			wvPallet.setStrList(uuidList);
			cnd.andIn("palletUuid", "strList", uuidList);
			wvPallet.setJhCnd(cnd);
			jhUpdate.add("uploadStatus").add("uploadTime");
			wvPallet.setUploadStatus(EWvPallet.uploadStatus_3.getKey());
			wvPallet.setUploadTime(new Date());
			wvPallet.setJhUpdate(jhUpdate);
			this.wvPalletService.updateSame(wvPallet);
			taskUuidList.clear();
			uuidList.clear();
		}
		list.clear();
		list=null;
	}

	/**
	 * 先同步环节数据
	 * @param dateInfo
	 */
	public void updateSynchronousWvProcess(WvCloudAccessConfig model,Map<String,String> dateMap,JhHttpClientUtil hd){
		WvUploadTaskInfo  wvUploadTaskInfo=new WvUploadTaskInfo();
		String dtUrl="app/ctws/public/syn/updateSynchronousProcess";

		JhCnd cnd=new JhCnd();
		cnd.andEquals("taskType");
		wvUploadTaskInfo.setTaskType(EWvUploadTaskInfo.taskType_4.getKey());
		wvUploadTaskInfo.setJhCnd(cnd);
		JingHaoPageInfo page=new JingHaoPageInfo();
		page.setLimit(200);
		wvUploadTaskInfo.setPage(page);
		List<String> uuidList=new ArrayList<String>();
		List<String> taskUuidList=new ArrayList<String>();
		List<Map<String,Object>> list=this.wvUploadTaskInfoService.queryMapListById(wvUploadTaskInfo, "queryMapListToWorkUuid");
		WvProcess wvProcess=new WvProcess();
		JhUpdate jhUpdate=new  JhUpdate();
		int i=0;
		List<Map<String,Object>> listProcess=null;
		for(Map<String,Object> map:list){
			uuidList.add(map.get("workUuid").toString());
			taskUuidList.add(map.get("taskUuid").toString());
			i++;
			if(i==20){
				cnd.reset();
				wvProcess.setStrList(uuidList);
				cnd.andIn("processUuid", "strList", uuidList);
				wvProcess.setJhCnd(cnd);
				wvProcess.setPage(null);
				listProcess=this.wvProcessService.queryMapListById(wvProcess,"queryMapListToCloud");
				if(listProcess!=null&&listProcess.size()>0){
					dateMap.put("jhPublicData", JSON.toJSONString(listProcess));
					this.saveSynchronousDate(model, dateMap, hd,dtUrl);
				}

				wvUploadTaskInfo.setStrList(taskUuidList);
				cnd.reset();
				cnd.andIn("taskUuid", "strList", taskUuidList);
				wvUploadTaskInfo.setJhCnd(cnd);
				this.wvUploadTaskInfoService.deleteSame(wvUploadTaskInfo);

				//更新托盘状态
				cnd.reset();
				wvProcess.setStrList(uuidList);
				cnd.andIn("processUuid", "strList", uuidList);
				wvProcess.setJhCnd(cnd);
				jhUpdate.add("uploadStatus").add("uploadTime");
				wvProcess.setUploadStatus(EWvProcess.uploadStatus_3.getKey());
				wvProcess.setUploadTime(new Date());
				wvProcess.setJhUpdate(jhUpdate);
				this.wvProcessService.updateSame(wvProcess);

				uuidList.clear();
				taskUuidList.clear();
				i=0;
			}
		}
		if(i>0){
			cnd.reset();
			wvProcess.setStrList(uuidList);
			cnd.andIn("processUuid", "strList", uuidList);
			wvProcess.setJhCnd(cnd);
			wvProcess.setPage(null);
			listProcess=this.wvProcessService.queryMapListById(wvProcess,"queryMapListToCloud");
			if(listProcess!=null&&listProcess.size()>0){
				dateMap.put("jhPublicData", JSON.toJSONString(listProcess));
				this.saveSynchronousDate(model, dateMap, hd,dtUrl);
			}
			wvUploadTaskInfo.setStrList(taskUuidList);
			cnd.reset();
			cnd.andIn("taskUuid", "strList", taskUuidList);
			wvUploadTaskInfo.setJhCnd(cnd);
			this.wvUploadTaskInfoService.deleteSame(wvUploadTaskInfo);

			//更新托盘状态
			cnd.reset();
			wvProcess.setStrList(uuidList);
			cnd.andIn("processUuid", "strList", uuidList);
			wvProcess.setJhCnd(cnd);
			jhUpdate.add("uploadStatus").add("uploadTime");
			wvProcess.setUploadStatus(EWvProcess.uploadStatus_3.getKey());
			wvProcess.setUploadTime(new Date());
			wvProcess.setJhUpdate(jhUpdate);
			this.wvProcessService.updateSame(wvProcess);
		}
		list.clear();
		list=null;
	}

	/**
	 * 先同步产品数据
	 * @param dateInfo
	 */
	public void updateSynchronousWvProduct(WvCloudAccessConfig model,Map<String,String> dateMap,JhHttpClientUtil hd){
		WvUploadTaskInfo  wvUploadTaskInfo=new WvUploadTaskInfo();
		String dtUrl="app/ctws/public/syn/updateSynchronousProduct";

		JhCnd cnd=new JhCnd();
		cnd.andEquals("taskType");
		wvUploadTaskInfo.setTaskType(EWvUploadTaskInfo.taskType_5.getKey());
		wvUploadTaskInfo.setJhCnd(cnd);
		JingHaoPageInfo page=new JingHaoPageInfo();
		page.setLimit(200);
		wvUploadTaskInfo.setPage(page);
		List<String> uuidList=new ArrayList<String>();
		List<String> taskUuidList=new ArrayList<String>();
		List<Map<String,Object>> list=this.wvUploadTaskInfoService.queryMapListById(wvUploadTaskInfo, "queryMapListToWorkUuid");
		WvProduct wvProduct=new WvProduct();
		JhUpdate jhUpdate=new  JhUpdate();
		int i=0;
		List<Map<String,Object>> listProduct=null;
		for(Map<String,Object> map:list){
			uuidList.add(map.get("workUuid").toString());
			taskUuidList.add(map.get("taskUuid").toString());
			i++;
			if(i==20){
				cnd.reset();
				wvProduct.setStrList(uuidList);
				cnd.andIn("productUuid", "strList", uuidList);
				wvProduct.setJhCnd(cnd);
				wvProduct.setPage(null);
				listProduct=this.wvProductService.queryMapListById(wvProduct,"queryMapListToCloud");
				if(listProduct!=null&&listProduct.size()>0){
					dateMap.put("jhPublicData", JSON.toJSONString(listProduct));
					this.saveSynchronousDate(model, dateMap, hd,dtUrl);
				}


				wvUploadTaskInfo.setStrList(taskUuidList);
				cnd.reset();
				cnd.andIn("taskUuid", "strList", taskUuidList);
				wvUploadTaskInfo.setJhCnd(cnd);
				this.wvUploadTaskInfoService.deleteSame(wvUploadTaskInfo);


				//更新托盘状态
				cnd.reset();
				wvProduct.setStrList(uuidList);
				cnd.andIn("productUuid", "strList", uuidList);
				wvProduct.setJhCnd(cnd);
				jhUpdate.add("uploadStatus").add("uploadTime");
				wvProduct.setUploadStatus(EWvProduct.uploadStatus_3.getKey());
				wvProduct.setUploadTime(new Date());
				wvProduct.setJhUpdate(jhUpdate);
				this.wvProductService.updateSame(wvProduct);

				taskUuidList.clear();
				uuidList.clear();
				i=0;
			}
		}
		if(i>0){
			cnd.reset();
			wvProduct.setStrList(uuidList);
			cnd.andIn("productUuid", "strList", uuidList);
			wvProduct.setJhCnd(cnd);
			wvProduct.setPage(null);
			listProduct=this.wvProductService.queryMapListById(wvProduct,"queryMapListToCloud");
			if(listProduct!=null&&listProduct.size()>0){
				dateMap.put("jhPublicData", JSON.toJSONString(listProduct));
				this.saveSynchronousDate(model, dateMap, hd,dtUrl);
			}

			wvUploadTaskInfo.setStrList(taskUuidList);
			cnd.reset();
			cnd.andIn("taskUuid", "strList", taskUuidList);
			wvUploadTaskInfo.setJhCnd(cnd);
			this.wvUploadTaskInfoService.deleteSame(wvUploadTaskInfo);


			//更新托盘状态
			cnd.reset();
			wvProduct.setStrList(uuidList);
			cnd.andIn("productUuid", "strList", uuidList);
			wvProduct.setJhCnd(cnd);
			jhUpdate.add("uploadStatus").add("uploadTime");
			wvProduct.setUploadStatus(EWvProduct.uploadStatus_3.getKey());
			wvProduct.setJhUpdate(jhUpdate);
			wvProduct.setUploadTime(new Date());
			this.wvProductService.updateSame(wvProduct);

			taskUuidList.clear();
			uuidList.clear();
		}
		list.clear();
		list=null;

	}
	/**
	 * http请求云端保存
	 * @param model
	 * @param dateMap
	 * @param hd
	 * @param dtUrl
	 */
	public void  saveSynchronousDate(WvCloudAccessConfig model,Map<String,String> dateMap,JhHttpClientUtil hd,String dtUrl){
		String httpUrl=model.getAddress();
		if(httpUrl.substring(httpUrl.length()-1, httpUrl.length()).equals("/")){
			httpUrl=model.getAddress()+""+dtUrl;
		}else{
			httpUrl=model.getAddress()+"/"+dtUrl;
		}
		String msg= hd.post(httpUrl, dateMap);
		this.checkResult(msg);
	}


	//==================定制内容结束======================
}
